package com.zj.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Array;
import java.util.Arrays;

//aop 2
@Component
@Aspect
public class Advice {
    //aop 3 匹配业务层的所有方法
    @Pointcut("execution(* com.zj.service.*Service.*(..))")
    private void servicePt(){
    }
    //aop 4  设置通知类型
    @Around(value = "servicePt()")
    public Object runSpeed(ProceedingJoinPoint pjp) throws Throwable {
        //得到签名
        Signature signature = pjp.getSignature();
        String className = signature.getDeclaringTypeName();
        String methodName = signature.getName();
        //获取请求参数
        Object[] objs = pjp.getArgs();
        System.out.println("请求参数" + Arrays.toString(objs));
        //返回值
        Object obj = null;
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            obj  = pjp.proceed();
        }
        long end = System.currentTimeMillis();
        System.out.println("业务层接口" + className + "." +methodName + "万次执行时间："+(end - start) + "ms");
        return obj;
    }
}
